package com.gun.management.module.raffle.dal.mysql.lotteryrecord;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.mybatis.core.mapper.BaseMapperX;
import com.gun.management.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.gun.management.module.raffle.controller.admin.lotterylog.vo.LotteryLogRespVO;
import com.gun.management.module.raffle.controller.admin.lotteryrecord.vo.LotteryRecordPageReqVO;
import com.gun.management.module.raffle.controller.admin.lotteryrecord.vo.LotteryRecordRespVO;
import com.gun.management.module.raffle.controller.app.vo.AppUserLatestLotteryRespVO;
import com.gun.management.module.raffle.controller.app.vo.AppUserLotterySummaryRespVO;
import com.gun.management.module.raffle.dal.dataobject.lottery.LotteryDO;
import com.gun.management.module.raffle.dal.dataobject.lotteryrecord.LotteryRecordDO;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;

/**
 * 用户抽奖履历 Mapper
 *
 * @author 测试账号
 */
@Mapper
public interface LotteryRecordMapper extends BaseMapperX<LotteryRecordDO> {

    default PageResult<LotteryRecordDO> selectPage(LotteryRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<LotteryRecordDO>()
                .eqIfPresent(LotteryRecordDO::getUserId, reqVO.getUserId())
                .eqIfPresent(LotteryRecordDO::getLotteryId, reqVO.getLotteryId())
                .eqIfPresent(LotteryRecordDO::getRuleId, reqVO.getRuleId())
                .eqIfPresent(LotteryRecordDO::getTotalTimes, reqVO.getTotalTimes())
                .eqIfPresent(LotteryRecordDO::getDayTotalTimes, reqVO.getDayTotalTimes())
                .eqIfPresent(LotteryRecordDO::getExpenseScore, reqVO.getExpenseScore())
                .eqIfPresent(LotteryRecordDO::getLeftScore, reqVO.getLeftScore())
                .eqIfPresent(LotteryRecordDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(LotteryRecordDO::getLotteryTime, reqVO.getLotteryTime())
                .betweenIfPresent(LotteryRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(LotteryRecordDO::getId));
    }

    @Insert("INSERT INTO  raffle_lottery_record(  user_id ,  lottery_id ,  rule_id ,  total_times ,    day_total_times ,    " +
            " expense_score ,  left_score ,  status ,  lottery_time ,     creator ,  create_time ,  updater ,  update_time )  " +
            "VALUES ( #{entity.userId}, #{entity.lotteryId}, #{entity.ruleId}, #{entity.totalTimes},   #{entity.dayTotalTimes},    " +
            "#{entity.expenseScore}, #{entity.leftScore}, #{entity.status}, #{entity.lotteryTime},   #{entity.creator}, #{entity.createTime}, #{entity.updater}, #{entity.updateTime});")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void insertAndGetId(@Param("entity") LotteryRecordDO entity);


    @Select({"<script>  " +
            " SELECT " +
            " A.id,A.user_id, " +
            " B.category_id lotteryCategoryId, " +
            " B.`name` lotteryName, " +
            " C.name prizeName, " +
            " C.category_id prizeType, " +
            " D.nickname nick_name," +
            " D.mobile," +
            " A.expense_score, " +
            " A.status, " +
            " A.lottery_time " +
            " FROM " +
            " raffle_lottery_record A " +
            " LEFT JOIN raffle_lottery B ON A.lottery_id = B.id " +
            " AND A.tenant_id = B.tenant_id " +
            " LEFT JOIN member_user D ON A.user_id = D.id " +
            " AND A.tenant_id = D.tenant_id " +
            " LEFT JOIN raffle_lottery_rule C ON A.rule_id = C.id " +
            " AND A.tenant_id = C.tenant_id " +
            "<where>" +
            "<if test='lotteryCategoryId != null  '>" +
            " and B.category_id =  #{lotteryCategoryId}  " +
            "</if>" +
            "<if test='prizeCategoryId != null '>" +
            " and C.category_id =  #{prizeCategoryId}  " +
            "</if>" +
            "<if test='prizeStatus != null '>" +
            " and A.status =  #{prizeStatus}  " +
            "</if>" +

            "<if test='nickName != null and nickName != \"\"'>" +
            " and D.nickname like concat('%',#{nickName},'%')" +
            "</if>" +

            "<if test='lotteryName != null and lotteryName != \"\"'>" +
            " and  B.`name`  like concat('%',#{lotteryName},'%')" +
            "</if>" +

            "<if test='prizeName != null and prizeName != \"\"'>" +
            " and  C.`name`  like concat('%',#{prizeName},'%')" +
            "</if>" +

            "<if test='lotteryTime != null '>" +
            " and A.lottery_time  between  #{lotteryTime[0]} and #{lotteryTime[1]}  " +
            "</if>" +
            "</where>" +
            " order by A.lottery_time desc,A.id desc " +
            "</script>"}
    )
    public IPage<LotteryRecordRespVO> selectLotteryRecordPageList(IPage<?> page, @Param("lotteryCategoryId") Long  lotteryCategoryId , @Param("prizeCategoryId") Long prizeCategoryId, @Param("prizeStatus") Integer  prizeStatus , @Param("lotteryTime") LocalDateTime[] lotteryTime,@Param("lotteryName") String lotteryName,@Param("prizeName") String prizeName,@Param("nickName") String nickName);


    @Select({"<script>SELECT " +
            " A.id," +
            " A.left_score," +
            " ( SELECT L.total_times FROM raffle_lottery L WHERE L.id = A.lottery_id ) -(" +
            " SELECT" +
            " ifnull( count( 1 ), 0 )" +
            " FROM" +
            " raffle_lottery_record r " +
            " WHERE " +
            " r.user_id = A.user_id " +
            " AND r.lottery_id = A.lottery_id " +
            " ) left_times," +
            " ( SELECT L.day_total_times FROM raffle_lottery L WHERE L.id = A.lottery_id ) -( " +
            " SELECT " +
            " ifnull( count( 1 ), 0 )" +
            " FROM " +
            " raffle_lottery_record r " +
            " WHERE " +
            " r.user_id = A.user_id " +
            " AND r.lottery_id = A.lottery_id " +
            " AND r.lottery_time   between  #{startOfDay} and  #{endOfDay}" +
            " ) day_left_times," +
            " ( SELECT L.bg_picture FROM raffle_lottery L WHERE L.id = A.lottery_id ) bg_picture," +
            " A.expense_score" +
            " FROM" +
            " raffle_lottery_record A " +
            "<where>" +
            "<if test='userId != null '>" +
            " and A.user_id =  #{userId}  " +
            "</if>" +
            "<if test='lotteryId != null '>" +
            " and A.lottery_id =  #{lotteryId}  " +
            "</if>" +
            " and A.lottery_time   between  #{startOfDay} and  #{endOfDay}" +
            "</where>" +
            " order  by A.lottery_time desc limit 1 " +
            "</script>"}
    )
    public AppUserLatestLotteryRespVO selectLatestLotteryRecord(@Param("lotteryId")  Long lotteryId, @Param("userId")  Long userId,@Param("startOfDay") LocalDateTime startOfDay, @Param("endOfDay") LocalDateTime endOfDay);



    @Select({"<script>  select A.lottery_id, A.user_id,count(1) userDayLotteryTimes  from raffle_lottery_record A " +
            " where  A.user_id =  #{userId}  and A.lottery_id =  #{lotteryId} "  +
            " and A.lottery_time   between  #{startOfDay} and  #{endOfDay}" +
            " group by A.lottery_id,A.user_id   "  +
            "</script>"}
    )
    public AppUserLotterySummaryRespVO selectUserLotterySummaryRecord(@Param("lotteryId")  Long lotteryId, @Param("userId")  Long userId, @Param("startOfDay") LocalDateTime startOfDay, @Param("endOfDay") LocalDateTime endOfDay);



}